package com.lnsf.bookstore.system.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.dto.ResultFailDto;
import com.lnsf.bookstore.common.exception.RException;
import com.lnsf.bookstore.system.order.dao.OrderMapper;
import com.lnsf.bookstore.system.order.service.OrderService;
import com.lnsf.bookstore.system.order.vo.Order;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageInfo<Order> selectAll(Map<String, Object> map, Order order) {
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
        return new PageInfo<Order>(orderMapper.selectAll());
    }

    @Override
    public int update(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int sendBooks(Order order) {

        Integer payStatus = order.getPayStatus();//付款状态
        if (1 != payStatus) {
            throw new RException("此订单未付款！");
        }
        order.setOrderStatus(3);//订单已发货
        /*order.setShippingStatus(1);//已发货*/
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Order selectByOId(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void cancelOrder() {
        long count = orderMapper.countBeforeOrderBySecond(3600L, 0);
        //log.info("取消超时未支付订单定时任务统计数量：" + count);
        if (count > 0) {
            long result = orderMapper.updateBeforeOrderBySecond(3600L, 7);
            //log.info("取消超时未支付订单定时任务取消数量：" + result);
        }
    }
}
